#!/usr/bin/perl

#Copyright (C) 2009  Tiziano Fogli, Simone Bernini

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# fogli@cs.unibo.it
# sbernini@cs.unibo.it


use strict;
use Switch;
use FileScalar;

#fornisce metodi per recuperare file scalar di interesse
package FileScalarPeer;

	#crea il collettore di oggetti FileScalar
	sub new {
		my ($class, $risDir) = @_;
		defined($risDir) or die("Files Scalar directory not defined");
		opendir(RISDIR, $risDir) or die("Files Scalar directory not exists");	close(RISDIR);

		my $self = {
			_simDir	=> $risDir,
			_files 	=> undef	
		};
		bless $self, $class;

		my @filesScalarOb;
		my @filesInRisDir = `find $risDir -name "*.sca"`;
		foreach my $filePath (@filesInRisDir) {
			chomp($filePath);
			push(@{$self->{_files}}, new FileScalar($filePath));
		}
		
		return $self;
	}

	#restituisce una hash di vettori ordinati.
        #una chiave per ogni vettore
        #o una sola chiave "nogroup" se non è stato richiesto il raggruppamento
	#input: il paramtro in base a cui raggruppare
	#input: il parametro in base a cui ordinare
	sub getSortedGroupedFilesScalar {
		my ($self, $orderBy, $groupBy, $zBy, %filters) = @_;
		defined($orderBy) or die("FileScalarPeer::getSortedGroupedFilesScalar(): missing orderBy parameters");
		
        if(scalar(%filters) eq 0) {
            print "Filters not found. Using all the files in the dir.\n";
        }

		my %result;
		if(defined($groupBy)) {
			foreach	my $fs (@{$self->{_files}}) {
                if($fs->checkUtility(%filters) eq 1) {
				    push(@{$result{$fs->getParameter($groupBy)}}, $fs);
                }
			}
            if(not(defined $zBy)) {
                push(@{$result{"x"}}, $self->getXByFileName($orderBy, 1, %filters));
            } else {
                push(@{$result{"x"}}, $self->getXByFileName($orderBy, 1, %filters));
            }
		} else {
			@{$result{"nogroup"}} = @{$self->{_files}};
		}	

		foreach my $group (keys(%result)) {
            my $param = "";
            if(defined($zBy)) {
                $param = $zBy
            } else {
                $param = $orderBy
            }
			@{$result{$group}} = sort {$a->getParameter($param) <=> $b->getParameter($param)} @{$result{$group}} if(not($group eq "x"));
		}

		return %result;
	}	

        sub getXByFileName {
            my ($self, $param, $distinct, %filters) = @_;

            my @X;

            foreach my $fs (@{$self->{_files}}) {
                if($fs->checkUtility(%filters)) {
                    my $find = 0;
                    foreach my $x (@X) {
                        if ($fs->getParameter($param) eq $x) {
                            $find++;
                        }
                    }            
                    if($distinct == 1) {   
                        if($find == 0) {
                            push(@X, $fs->getParameter($param));
                        }
                    } else {
                        push(@X, $fs->getParameter($param));
                    }
                }
            }

            @X = sort { $a <=> $b } @X;
#            print "x for current graphic:";
#            foreach my $app (@X) {
#                printf "$app ";
#            }
#            print "\n";

            return @X;
        }

1;
